import {
	defineStore
} from 'pinia';
import {
	toRefs,
	reactive
} from 'vue'

import {
	getAgentMultilevelList,
	queryFCustAddressByCustId,
	findSysDictDetailByType,
	getAgentStaffList
} from '@/api/common.js'

export default defineStore('selectOptions', () => {
	const state = reactive({
		subAgentList: [], //子公司
		addressAliasList: [], // 围栏
		vehicleTypeList: [], //车型
		// 告警类型
		warningTypeList: [{
				code: 'OFFLINE_WARNING',
				name: '离线告警'
			},
			{
				code: 'NO_ENTRY',
				name: '禁行告警'
			},
			{
				code: 'NO_PARKING',
				name: '禁停告警'
			},
			{
				code: 'SPEED_LIMIT',
				name: '超速告警'
			},
			{
				code: 'GATHER',
				name: '聚集告警'
			},
			{
				code: 'FATIGUE_DRIVING',
				name: '疲劳告警'
			},
			{
				code: 'VIDEO_PARKING_WARNING',
				name: '黑点视频违停'
			},
		],
		agentStaffList: []
	});


	// 查子公司
	async function getSubAgentList() {
		const agentId = uni.getStorageSync('_agentId');
		const {
			agent,
			returnCode,
			subAgentList //子公司
		} = await getAgentMultilevelList(agentId).catch(() => {
			state.subAgentList = []
		})

		if (returnCode == 'Y') {
			state.subAgentList = subAgentList
			console.log("===subAgentList", subAgentList)
		} else {
			state.subAgentList = []
		}
	}
	// 查围栏列表
	async function getAddressAliasList() {
		const {
			obj,
			returnCode,
		} = await queryFCustAddressByCustId().catch(() => {
			state.addressAliasList = []
		})

		if (returnCode == 'Y') {
			state.addressAliasList = obj
		} else {
			state.addressAliasList = []
		}
	}
	// 查车型
	async function getVehicleTypeList() {
		const {
			code,
			results,
		} = await findSysDictDetailByType().catch(() => {
			state.vehicleTypeList = []
		})

		if (code == 0) {
			state.vehicleTypeList = results.vehicleType
		} else {
			state.vehicleTypeList = []
		}
	}
	// 查围栏用户
	async function getAgentUserList() {
		const {
			returnCode,
			obj,
		} = await getAgentStaffList().catch(() => {
			state.agentStaffList = []
		})

		if (returnCode == 'Y') {
			state.agentStaffList = obj
		} else {
			state.agentStaffList = []
		}
	}

	return {
		...toRefs(state),
		getSubAgentList,
		getAddressAliasList,
		getVehicleTypeList,
		getAgentUserList
	}
})

// 使用
// <script setup>
// import { storeToRefs } from 'pinia'
// const store = useCounterStore()
// // `name` 和 `doubleCount` 是响应式的 ref
// // 同时通过插件添加的属性也会被提取为 ref
// // 并且会跳过所有的 action 或非响应式 (不是 ref 或 reactive) 的属性
// const { count, doubleCount } = storeToRefs(store)
// // 作为 action 的 increment 可以直接解构
// const { increment } = store
// </script>